﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Web.Script.Serialization; 

namespace Totoro.Passport
{
    [Serializable ]
    public class Ticket
    {
        private string ip;
        private string userName;
        private string userID;
        private DateTime expiredTime;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="ip"></param>
        /// <param name="userName"></param>
        /// <param name="userID"></param>
        public Ticket(string ip, string userName, string userID)
        {
            this.ip = ip;
            this.userName = userName;
            this.userID = userID;
            this.expiredTime = DateTime.Now.AddSeconds(double.Parse (Configuration.TotoroConfiguration.GetConfig().TickExpireTime.Value) );

        }
        public Ticket() { }

        public string IP
        {
            get { return ip; }
            set { ip = value; }
        }
        public string UserName
        {
            get { return userName; }
            set { userName = value; }  
        }
        public string UserID
        {
            get { return userID; }
            set { userID = value; }
        }
        public DateTime ExpiredTime
        {
            get { return expiredTime; }
            set { expiredTime = value; }
        }


        public bool IsValid()
        {
            if (ExpiredTime.ToLocalTime() > DateTime.Now)
            {
                ExpiredTime = DateTime.Now;
                return true;
            }
            else return false;
        }
        public string SerializeTicketToStr()
        {
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            return serializer.Serialize(this);
            
        }

        public static Ticket GetTicketFromStr(string str )
        {
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            Ticket t = serializer.Deserialize<Ticket>(str);
            return t; 
        }
        
        //public static Ticket CreateTicket(string userID, string userName, string ip)
        //{
        //    //Ticket t = new Ticket();
        //    //t.UserID = userID;
        //    //t.UserName = userName;
        //    //t.ExpiredTime = DateTime.Now;
        //    //t.IP = ip;
        //    //return t;

        //}
    }
}
